package helloworld;

import helloworld.Vo.ParkIdUserName;
import helloworld.Vo.ParkList;
import helloworld.dao.*;
import helloworld.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by admin on 2016/11/8.
 */
@RestController
public class GreetingController {
    @Autowired
    demoDao my_demoDao;
    @Autowired
    demo2Dao my_demo2Dao;

    @Autowired
    MyUserDao myUserDao;
    @Autowired
    ParkingLotDao parkingLotDao;
    @Autowired
    ReceiptDao receiptDao;
    @Autowired
    UserParkDao userParkDao;


    @RequestMapping("/hello")
    public String greeting(String fuck)
    {

        System.out.println(fuck);
        demo2 temp=new demo2();
        demo t=my_demoDao.findOne(2);
        return "greeting";
    }

    @RequestMapping("/getParkUser")
    public ParkList getParkUser()
    {
        List<ParkIdUserName> temp=new ArrayList<ParkIdUserName>();
        List<UserPark> list=userParkDao.findAll();
        for(UserPark userPark:list)
        {
            if(userPark.getParking_lot().getKind().equals("park"))
            {
                ParkIdUserName k=new ParkIdUserName();
                k.setParkId(String.valueOf(userPark.getParking_lot().getId()));
                k.setUserName(userPark.getMyUser().getWeixin_name());
                k.setParkName(userPark.getParking_lot().getName());
                temp.add(k);
            }
        }
        ParkList parkList=new ParkList();
        parkList.setList(temp);
        return parkList;
    }

    @RequestMapping("/doorOpenCheck")
    public Integer fuck()
    {
        Parking_lot parking_lot=parkingLotDao.getOneDoor("door","open");
        if (parking_lot!=null)
        {
            parking_lot.setFlag("close");
            parkingLotDao.save(parking_lot);
            return parking_lot.getId();
        }

            return null;
    }

    @RequestMapping(value = "/opendoor",method = RequestMethod.GET)
    public String open(String nickname,String parkid,String kind)//开门接口用于door和parkinglot
    {

        MyUser temp=myUserDao.getUserByName(nickname);
        String userid=String.valueOf(temp.getId());
        UserPark userPark=userParkDao.getUserPark(Integer.valueOf(userid),Integer.valueOf(parkid));
        if(userPark==null)
            return "no";

        if(kind.equals("door"))
        {
            Parking_lot parking_lot=parkingLotDao.getOne(Integer.valueOf(parkid));
            parking_lot.setFlag("open");
            return "yes";
        }

        if(kind.equals("park"))
        {
            MyUser myUser=myUserDao.getOne(Integer.valueOf(userid));//获取user
            Parking_lot parking_lot=parkingLotDao.getOne(Integer.valueOf(parkid));//获取parkinglot
            Receipt receipt=new Receipt();//新建账单
            receipt.setMyUser(myUser);
            receipt.setCreated_time(new Date());
            receipt.setParking_lot(parking_lot);
            receipt=receiptDao.save(receipt);
            myUser.setReceipt(receipt);//持久化账单
            myUserDao.save(myUser);//将最新的账单存进user
            return "yes";
        }

        return "no";
    }
    @RequestMapping(value = "/closedoor",method = RequestMethod.GET)
    public String close(String nickname,String parkid,String kind)//离开开门接口用于parkinglot
    {
        MyUser temp=myUserDao.getUserByName(nickname);
        String userid=String.valueOf(temp.getId());
        UserPark userPark=userParkDao.getUserPark(Integer.valueOf(userid),Integer.valueOf(parkid));
        if(userPark==null)
            return "no";

        if(kind.equals("door"))
        {
            return "no";
        }

        if(kind.equals("park"))
        {
            MyUser myUser=myUserDao.getOne(Integer.valueOf(userid));//获取user
            Parking_lot parking_lot=parkingLotDao.getOne(Integer.valueOf(parkid));//获取parkinglot
            Receipt receipt=myUser.getReceipt();//获取账单
            if(receipt==null)
                return "no";
            receipt.setEnd_time(new Date());//加入离开时间
            receipt=receiptDao.save(receipt);
            myUser.setReceipt(null);//删除User的账单
            myUserDao.save(myUser);//将最新的账单存进user
            return "yes";
        }

        return "no";
    }
}
